const app = getApp();
const http = require('../../utils/http')
Page({
      data: {
            weekList: [
                  // '星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六',
                  {name: '星期日', id: 7, selected: false},
                  {name: '星期一', id: 1, selected: false},
                  {name: '星期二', id: 2, selected: false},
                  {name: '星期三', id: 3, selected: false},
                  {name: '星期四', id: 4, selected: false},
                  {name: '星期五', id: 5, selected: false},
                  {name: '星期六', id: 6, selected: false},
            ],
            chooseWeekList: [],
            certainTimesIndex: null,
            ListIndex: ['', '全部时间段', '按周'],
            multiArray: [[], [], []],
            selectIndex: null,
            showCategoryName: '',
            haveResume: false,
            showData: {},

            query: {
                  receivingType: 1,
                  orderPrice: "",
                  orderTime: [],
                  weekTime: [],
                  categoryId: '',
                  parentId: '',
                  yeCategoryId: ''


            }
      },
      onLoad: function (options) {
            // advantage: "英语专业八级证书"
            // age: 29
            // categoryId: 135
            // categoryName: "英语口语"
            // courseDesc: "Mr题题题而牛股鹅绒修护"
            // createTime: "2021-01-20 02:19:22"
            // headImg: "https://eco-culture.oss-cn-shenzhen.aliyuncs.com/images/bear/t2.jpg"
            // id: 54
            // introduce: "在工作中细致认真，乐于接受挑战；有良好的沟通能力，注重团队合作，在积极听取他人意见的同时也会提出自己的见解。英语口语能力突出，英国剑桥大学毕业。"
            // limitCount: 0
            // mobile: null
            // name: "刘莹"
            // nickName: null
            // orderNum: null
            // orderPrice: 32000
            // orderTime: "9,10,11,12,13,14,15,16,17,18,19,20,21"
            // other: "雅思口语8分"
            // parentId: 116
            // parentName: "语种"
            // postgraduate: "国内在读研究生"
            // receivingType: 1
            // settingList: []
            // sort: 10
            // status: 1
            // teachAge: 7
            // undergraduate: "2010-2014英国剑桥大学"
            // updateTime: "2021-01-26 11:06:48"
            // userId: 39
            // weekTime: "1,2,3,4,5,6,7"
            // working: "中国国旅文案策划，陪同翻译"
            // yeCategoryId: 99
            // yeCategoryName: "文化类"
            // receivingType: 1


//             userId	是	int	用户ID
//             yeCategoryId	是	int	一级分类
//             parentId	是	int	二级分类
//             categoryId	是	int	三级分类
//             orderPrice	是	int	价格（单位分）
// weekTime	是	String	学历证书
//             receivingType	是	int	1:全部时间, 2:按周
//             orderTime	是	String	接单时间段
//             courseDesc	是	String	课程介绍
//             minimumHours

            if (Object.keys(app.globalData.resume).length > 0) {

                  console.log(app.globalData.resume, 'resume----')
                  let isShow = app.globalData.resume.yeCategoryId && app.globalData.resume.parentId

                  if (isShow) {
                        let cate = app.globalData.resume.yeCategoryName + "-" + app.globalData.resume.parentName + '-' + app.globalData.resume.categoryName
                        let time = app.globalData.resume.receivingType == 1 ? '全部时间段' : '按周';

                        this.setData({
                              "query.userId": app.globalData.userInfo.userId,
                              "query.yeCategoryId": app.globalData.resume.yeCategoryId,
                              "query.parentId": app.globalData.resume.parentId,
                              "query.categoryId": app.globalData.resume.categoryId,
                              "query.orderPrice": parseFloat(app.globalData.resume.orderPrice / 100).toFixed(2),
                              "query.weekTime": app.globalData.resume.weekTime,
                              "query.receivingType": app.globalData.resume.receivingType,
                              "query.orderTime": app.globalData.resume.orderTime,
                              "query.courseDesc": app.globalData.resume.courseDesc,
                              "query.limitCount": app.globalData.resume.limitCount,
                              'showData.cate': cate,

                              'showData.time': time,
                              haveResume: isShow
                        })
                  }

            }


            let arr = []
            for (let i = 1; i <= 24; i++) {
                  if (i < 10) {
                        arr.push('0' + i)
                  } else {
                        arr.push(i + "")
                  }
            }

            this.setData({
                  hour: arr,
                  hour2: arr,
            })

      },
      initData() {
            console.log(app.globalData.resume)
            if (!app.globalData.resume || Object.keys(app.globalData.resume).length == 0) {
                  return
            } else {
                  let data = this.data.query;
                  //
                  for (let key in data) {
                        if (key == 'orderPrice') {
                              data[key] = app.globalData.resume[key] / 100
                        } else if (key == 'orderTime') {
                              data[key] = app.globalData.resume.orderTime && app.globalData.resume.orderTime.split(',')
                        } else {
                              data[key] = app.globalData.resume[key];
                        }


                  }
                  let arr1 = this.data.weekList;
                  let weekTime = app.globalData.resume.weekTime && app.globalData.resume.weekTime.split(',')
                  for (let i = 0; i < arr1.length; i++) {
                        for (let j = 0; j < weekTime.length; j++) {
                              if (weekTime[j] == arr1[i].id) {
                                    arr1[i].selected = true
                              }

                        }

                  }
                  let str = app.globalData.resume.yeCategoryName + '-' + app.globalData.resume.parentName + '-' + app.globalData.resume.categoryName;

                  let chooseList = []
                  arr1.forEach((item) => {
                        if (item.selected) {
                              chooseList.push(item)
                        }
                  })
                  console.log(data, 'arr---------')
                  this.setData({
                        chooseWeekList: chooseList,
                        weekList: arr1,
                        showCategoryName: str,
                        query: data
                  })
            }

      },
      chooseTime() {
            this.setData({
                  showOrderTimeModal: true
            })
      },
      getCurrentCategory(data) {
            // categoryId: 101
            // parentId: 100
            // showCategoryName: "文化类-学前教育-幼师"
            // yeCategoryId: 99


            // categoryId: '',
            //     parentId: '',
            //     yeCategoryId: ''

            this.setData({
                  showCategoryName: data.detail.showCategoryName,
                  'query.categoryId': data.detail.categoryId,
                  'query.parentId': data.detail.parentId,
                  'query.yeCategoryId': data.detail.yeCategoryId,
            })
            console.log(data, 'data----')
      },
      getChangeValuse(e) {
            let key = e.currentTarget.dataset.item
            this.setData({
                  [`query.${key}`]: e.detail.value
            })

      },
      OnSubmit() {

            if (!this.data.query.yeCategoryId || !this.data.query.parentId || !this.data.query.categoryId) {
                  wx.showModal({
                        title: '提示',
                        content: '请选择所教的课程',
                        showCancel: false
                  })
                  return
            }
            if (!this.data.chooseWeekList) {
                  wx.showModal({
                        title: '提示',
                        content: '请选择上课时间',
                        showCancel: false
                  })
                  return
            }
            if (!this.data.query.orderPrice) {
                  wx.showModal({
                        title: '提示',
                        content: '请输入您的课程价格 ',
                        showCancel: false
                  })
                  return
            }
            if (!this.data.query.courseDesc) {
                  wx.showModal({
                        title: '提示',
                        content: '请输入您的课程介绍 ',
                        showCancel: false
                  })
                  return
            }
            let arr = [], current, diffence, timeArr = [];
            /*
            * 1.是全部时间段
            * 2：按周
            * */
            if (this.data.query.receivingType == 2) {

                  console.log(this.data.chooseWeekList, '阿里山的咖啡机')
                  this.data.chooseWeekList.forEach((week) => {
                        arr.push(week.id);
                  })

                  diffence = this.data.query.orderTime[this.data.query.orderTime.length - 1] - this.data.query.orderTime[0]
                  current = Number(this.data.query.orderTime[0])
                  for (let i = 0; i < diffence + 1; i++) {
                        timeArr.push(current++)
                  }

            } else {
                  let num = 9;
                  for (let i = 0; i < 13; i++) {
                        timeArr.push(num++)
                  }

                  arr = [1, 2, 3, 4, 5, 6, 7]
            }


            arr = arr.sort();
            this.data.query.orderTime = timeArr.toString()
            this.data.query.orderPrice = this.data.query.orderPrice * 100
            this.data.query.weekTime = arr.toString();

            // this.setData({
            //       "query.weekTime": arr.toString(),
            //       'query.orderPrice': this.data.query.orderPrice * 100
            // })
            // console.log(this.data.query, 'query--')

            http.request({
                  url: '/trainer/setTrainerInfo',
                  data: this.data.query,
                  callBack: (res) => {
                        this.getTeacherInfo()


                  }
            })
      },
      getTeacherInfo(id) {


            http.request({
                  method: 'GET',
                  url: '/apply/getTeacherResume',
                  data: app.globalData.userInfo.userId,
                  callBack: (res) => {
                        if (res.data) {
                              app.globalData.resume = res.data;

                        }
                        wx.showToast({
                              duration: 3000,
                              title: '设置成功',
                              success: (res) => {
                                    setTimeout(() => {
                                          wx.switchTab({
                                                url: '/pages/user/user'
                                          })
                                    }, 500)
                              }
                        })

                  }
            })
      },
      chooseWeekItem(e) {

            let index = Number(e.currentTarget.dataset.week);
            // 保存当前数组中的值
            let currentItemStatus = "weekList[" + index + "].selected";
            let currentItem = this.data.weekList[index]
            // 保存选中的星期数组
            let arr = this.data.chooseWeekList;
            this.setData({
                  [currentItemStatus]: !this.data.weekList[index].selected
            })
            if (this.data.weekList[index].selected) {

                  arr.push(currentItem)
                  this.setData({
                        chooseWeekList: arr
                  })
            } else {
                  // 去重删除取消选中的星期
                  arr.forEach((item, idx) => {
                        if (item.id == this.data.weekList[index].id) {
                              arr.splice(idx, 1)
                        }
                  })
                  this.setData({
                        chooseWeekList: arr
                  })
            }

            console.log(arr, 'arr--')

      },
      closeModal1() {
            this.setData({
                  showOrderTimeModal: false
            })
      },
      closeModal(e) {

            if (this.data.chooseWeekList.length == 0 && this.data.query.receivingType == 2) {
                  wx.showToast({
                        title: "请选择上课时间",
                        icon: 'none'
                  })
            }
            if (this.data.query.orderTime.length == 0 && this.data.query.receivingType == 2) {
                  wx.showToast({
                        title: "请选择上课时间",
                        icon: 'none'
                  })
            } else if (Number(this.data.query.orderTime[1]) < Number(this.data.query.orderTime[0]) && this.data.query.receivingType == 2) {
                  wx.showToast({
                        title: '结束时间不能小于开始时间',
                        icon: 'none'
                  })

            } else {
                  this.setData({
                        showOrderTimeModal: false
                  })
            }


      },

      // 获取当前输入的价格
      getOrderPrice(e) {
            let type = e.currentTarget.dataset.type;
            console.log(e.detail.value, '-----')
            this.setData({
                  [`query.${type}`]: e.detail.value
            })
      },
      getTimer(time, time1) {
            console.log(time, time1, 'time1---')
            let arr = this.data.query.orderTime;
            arr[0] = (time.detail.value[0] + 1).toString();
            this.setData({
                  orderTime: [...arr]
            })
      },
      getTimer2(time, time1) {
            let arr = this.data.query.orderTime;
            arr[1] = (time.detail.value[0] + 1).toString();
            this.setData({
                  orderTime: [...arr]
            })
            console.log(this.data.query.orderTime, 'orderTime---')
      },
      // 点击模块时间分类事件
      chooseTimeTum(e) {


            this.setData({
                  'query.receivingType': e.currentTarget.dataset.cur,

            })
      },
      // 选择技能等级


});